
Imports System.Data
Partial Class Admin_frmTabProduct
    Inherits System.Web.UI.Page
    Dim conn As New SqlString()
    Dim _BO As New Bo()
    Dim _ProductVO As New ProductVo()
    Dim _SchemeVO As New SchemeVo
    Dim _EmployeeId As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        _EmployeeId = Session("EmpId")
        If _EmployeeId = "" Then
            Response.Redirect("../Client/frmLogin.aspx")
        End If
        If Not IsPostBack Then
            panel2.Visible = False
            panel4.Visible = False
            GridData()
            Categorydata()
            Schemedata()
            SubCategoryData()
            DescriptionData()
        End If
    End Sub
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Try
            Dim varPath As String
            _ProductVO.ProductId = txtProductId.Text
            _ProductVO.CategoryId = ddlCategoryId.SelectedValue
            _ProductVO.SubCategoryId = ddlSubCategoryId.SelectedValue
            _ProductVO.Price = txtPrice.Text
            _ProductVO.Brand = txtBrand.Text
            ' varPath = "F:/College/Projects/Backup/testing project/uploads/" + fuPhotoPath.FileName
            varPath = "../Uploads/" + fuPhotoPath.FileName
            _ProductVO.PhotoPath = varPath
            _ProductVO.SchemeId = ddlSchemeId.SelectedValue
            _ProductVO.TotalQuantity = txtTotalQuantity.Text
            _ProductVO.AvailableQuantity = lblAvailableQuantity.Text
            _ProductVO.Count = 0
            _BO.InsertProduct(_ProductVO)
            If (fuPhotoPath.HasFile) Then
                fuPhotoPath.SaveAs(Server.MapPath("../Uploads/" & fuPhotoPath.FileName))
            End If
            ClearAll()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Session("EmpId") = _EmployeeId
    End Sub
    Public Sub ClearAll()
        txtProductId.Text = ""
        txtPrice.Text = ""
        txtBrand.Text = ""
        txtTotalQuantity.Text = ""
        lblAvailableQuantity.Text = ""
        MsgBox("Product inserted")
    End Sub
    Public Sub Schemedata()
        Dim dtScheme As DataTable
        dtScheme = _BO.getScheme()
        ddlSchemeId.DataSource = dtScheme
        ddlSchemeId.DataValueField = dtScheme.Columns(0).ToString
        ddlSchemeId.DataTextField = dtScheme.Columns(0).ToString
        ddlSchemeId.DataBind()
    End Sub
    Public Sub Categorydata()
        Dim dtCategory As DataTable
        dtCategory = _BO.getcategory()
        ddlCategoryId.DataSource = dtCategory
        ddlCategoryId.DataValueField = dtCategory.Columns(0).ToString
        ddlCategoryId.DataTextField = dtCategory.Columns(1).ToString
        ddlCategoryId.DataBind()
    End Sub
    Sub SubCategoryData()
        Dim dtSubCategory As DataTable
        dtSubCategory = _BO.getsubcategory(ddlCategoryId.SelectedValue)
        ddlSubCategoryId.DataSource = dtSubCategory
        ddlSubCategoryId.DataValueField = dtSubCategory.Columns(1).ToString
        ddlSubCategoryId.DataTextField = dtSubCategory.Columns(2).ToString
        ddlSubCategoryId.DataBind()
    End Sub
    Sub DescriptionData()
        _SchemeVO = _BO.getDescription(ddlSchemeId.SelectedValue)
        txtDescription.Text = _SchemeVO.Description
    End Sub
    Protected Sub ddlCategoryId_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCategoryId.SelectedIndexChanged
        SubCategoryData()
    End Sub
    Protected Sub ddlSchemeId_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSchemeId.SelectedIndexChanged
        DescriptionData()
    End Sub

    Protected Sub txtTotalQuantity_lostfocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTotalQuantity.TextChanged
        lblAvailableQuantity.Text = txtTotalQuantity.Text
    End Sub
    Sub GridData()
        Dim dtProduct As DataTable
        dtProduct = _BO.getProduct()
        gdProduct.DataSource = dtProduct
        gdProduct.DataBind()
    End Sub
    Protected Sub gdProduct_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles gdProduct.EditCommand
        Session("EmpId") = _EmployeeId
        Response.Redirect("frmModifyProduct.aspx?ProductId=" & e.Item.Cells(0).Text)
    End Sub
    Protected Sub gdProduct_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles gdProduct.DeleteCommand
        _BO.DeleteProduct(e.Item.Cells(0).Text)
        Response.Redirect("frmTabProduct.aspx")
    End Sub
    Protected Sub ImageAdd_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageAdd.Click
        panel2.Visible = True
        panel4.Visible = False
    End Sub

    Protected Sub ImageEdit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageEdit.Click
        panel2.Visible = False
        panel4.Visible = True
        GridData()
    End Sub
    Protected Sub gdProduct_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles gdProduct.PageIndexChanged
        gdProduct.CurrentPageIndex = e.NewPageIndex
        GridData()
    End Sub

    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        txtProductId.Text = ""
        txtPrice.Text = ""
        txtBrand.Text = ""
        txtTotalQuantity.Text = ""
        lblAvailableQuantity.Text = ""
    End Sub
End Class

